<?php

class My_Wertex {

    function  __construct($id = null) {

        $this->tableName = 'wertexs';
        $this->table = new Zend_Db_Table($this->tableName);

        $info = $this->table->info();
        $this->primary = $info['primary'][1];

        $this->request = new Zend_Controller_Request_Http;

        $this->data = array('object' => '0', 'weight' => '0');

        if(is_array($id)){
            $id = $id[$this->primary];
        }
        if(!empty($id)){
            $this->setId($id);
            $this->readById();
        }
    }

    function setId($id){
        $this->id = $id;
    }

    function setData($data){
        if(is_array($data)){
            $this->data['object'] = $data['object'];
            $this->data['weight'] = $data['weight'];
        }
    }

    function getData(){
        return $this->data;
    }

    function getId(){
        return $this->id;
    }

    function getEnd(){
        return $this->data['end'];
    }

    function exists(){
        return $this->data[$this->primary] ? true : false;
    }

    function readById($id = null){
        if(!empty($id)){
            $this->setId($id);
        }
        $result = $this->table->find($this->getId());
        $data = $result->toArray();
        $this->data = $data[0];
    }

    function getAll(){
        $result = $this->table->fetchAll(
            $this->table->select()
                ->order('id')
            );
        return $result->toArray();
    }

    function getObject($id = null){
        if(!empty($id)){
            $this->setId($id);
        }
        if(!$this->exists()){
            $this->readById();
        }
        return $this->data['object'];
    }

    function getWeight($id = null){
        if(!empty($id)){
            $this->setId($id);
        }
        if(!$this->exists()){
            $this->readById();
        }
        return $this->data['weight'];
    }

    function insert($data = null){
        if(is_array($data)){
            $this->setData($data);
        }

        $id = $this->table->insert($this->getData());
        $this->setId($id);
        return $id;
    }

    /**
     * delete one row
     * 
     * @param <type> $data
     * @return int number of rows deleted
     */
    function delete($data = null){
        if(is_array($data)){
            $this->setData($data);
        }

        if($this->exists()){
            $where = $this->table->getAdapter()->quoteInto($this->primary.' = ?', $this->getId());
            return $this->table->delete($where);
        }
        return false;
    }
    
}
?>
